#!/bin/bash

# 参数DB_BACKUP_DIR,最好将备份目录指向磁盘空间大的目录
DB_BACKUP_DIR=/data/mariadb-backup
mkdir -p $DB_BACKUP_DIR
cd $DB_BACKUP_DIR
# 备份数据库须知：
#1 保证从本机能远程访问DB主机；
#2 本机已经安装mysqldump工具(mariadb包）；
#3 参数DB_BACKUP_NAME后面的名称前缀要不同，避免重名覆盖
#4 一般只需要修改参数MARIADB_CONN数据库连接信息，其他不需要修改
#5 默认备份数据库主机上除information_schema,performance_schema,test三个系统数据库以外的所有DB

# 备份云平台DB
# 第一个db连接信息
MARIADB_CONN1="-uroot -pmySECURITY_PASS119 -h172.25.0.18 "

MARIADB_SQL1="SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema','performance_schema','test')" 
MARIADB_FILE1=$DB_BACKUP_DIR/dbfile1.txt
mysql ${MARIADB_CONN1} -ANe"${MARIADB_SQL1}" > ${MARIADB_FILE1}
# 第一个db数据库 
DBLIST1=""
DB_BACKUP_NAME1=cloud-db-`date +%F-%H-%M-%S`
for DB in `cat ${MARIADB_FILE1}` ; do DBLIST1="${DBLIST1} ${DB}" ; done
mysqldump ${MARIADB_CONN1}  --databases ${DBLIST1} > $DB_BACKUP_NAME1.sql
gzip $DB_BACKUP_NAME1.sql &

# 第二个db连接信息
MARIADB_CONN2="-uroot -pmySECURITY_PASS119 -hmariadb.rscommon.svc.rs.local "

MARIADB_SQL2="SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema','performance_schema','test')" 
MARIADB_FILE2=$DB_BACKUP_DIR/dbfile2.txt
mysql ${MARIADB_CONN2} -ANe"${MARIADB_SQL2}" > ${MARIADB_FILE2}
# 第二个db数据库 
DBLIST2=""
DB_BACKUP_NAME2=app-db-`date +%F-%H-%M-%S`
for DB in `cat ${MARIADB_FILE2}` ; do DBLIST2="${DBLIST2} ${DB}" ; done
mysqldump ${MARIADB_CONN2}  --databases ${DBLIST2} > $DB_BACKUP_NAME2.sql
gzip $DB_BACKUP_NAME2.sql &

#如果需要备份更多主机上的DB，请自行添加
#删除90天之前的备份，请根据磁盘空间和客户的要求合理设置数值
find   $DB_BACKUP_DIR -atime +90 -print0 |xargs -0 rm -rf
#DB还原,需要先解压，再导入
# gunzip $DB_BACKUP_NAME1.sql.gz; mysql ${MARIADB_CONN1} <$DB_BACKUP_NAME1.sql
rsync -avl --delete-after /data/mariadb-backup 172.25.0.28:/data/backup/ceshi/

# minio备份，请修改变量
#MINIO_BACKUP_DIR=/data/minio-backup
#rsync -av MINIO_SRC_IP:/MINIO_SRC_PATH  $MINIO_BACKUP_DIR
rsync -avl /data/sto/minio 172.25.0.28:/data/backup/ceshi/
